package com;

import com.util.CopyDir;
import com.util.ItzCompress;

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Main extends Application {

    int index = 1;
    String themeDir = "";

    @Override
    public void start(Stage primaryStage) throws Exception {

        MainListenController listen = new MainListenController();

        BorderPane root = new BorderPane();
        root.setPadding(new Insets(20, 20, 20, 20));

        VBox leftVBox = new VBox();
        leftVBox.setPrefSize(400, 900);
        leftVBox.setStyle("-fx-border-color: black");
        leftVBox.setSpacing(20);

        VBox rightVBox = new VBox();
        rightVBox.setPrefSize(600, 900);
        rightVBox.setStyle("-fx-border-color: black");

        VBox topVBox = new VBox();
        topVBox.setPrefSize(1800, 50);
        topVBox.setStyle("-fx-border-color: black");

        VBox bottomVBox = new VBox();
        bottomVBox.setPrefSize(1800, 50);
        bottomVBox.setStyle("-fx-border-color: black");

        VBox centerVBox = new VBox();
        //centerVBox.setPrefSize(1000,1000);
        centerVBox.setStyle("-fx-border-color: black");
//        centerVBox.setStyle("-fx-background-color: black");

        Button itzDir = new Button("压缩成.itz");
        itzDir.setPrefSize(100, 50);
        itzDir.setVisible(false);


        //导入路径
        File fileDir = null;
        Button chooseDir = new Button("选择文件夹");
        chooseDir.setPrefSize(100, 50);
        Button pre = new Button("上一张");
        pre.setPrefSize(100, 50);
        pre.setVisible(false);
        Button next = new Button("下一张");
        next.setPrefSize(100, 50);
        next.setVisible(false);

        Button cutNode = new Button("截取预览图");
        cutNode.setPrefSize(100, 50);
        cutNode.setVisible(false);


        HBox hBox = new HBox();
        hBox.setSpacing(100);
        hBox.getChildren().addAll(pre, next);

        VBox vBox = new VBox();
        vBox.getChildren().add(new Text("请选择主题"));
        Text text = new Text();
        text.setFont(Font.font(null,20));
        leftVBox.getChildren().addAll(chooseDir,text, hBox);
        rightVBox.getChildren().add(vBox);

        Text text2 = new Text();
        text2.setFont(Font.font(null,20));

        centerVBox.getChildren().addAll(cutNode,text2);

        Text text1 = new Text();
        text1.setFont(Font.font(null,20));
        HBox hBoxItz = new HBox(itzDir,text1);

        topVBox.getChildren().add(hBoxItz);
        bottomVBox.getChildren().add(new Label("zxc"));

        chooseDir.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                DirectoryChooser chooser = new DirectoryChooser();
                File fileDir = chooser.showDialog(primaryStage);
                if (fileDir != null) {
                    themeDir = fileDir.getAbsolutePath();
                    boolean bool = checkDir(themeDir);
                    if (bool) {
                        itzDir.setVisible(true);
                        pre.setVisible(false);
                        next.setVisible(true);
                        cutNode.setVisible(true);
                        index = 1;
                        listen.dirListener(themeDir, rightVBox);
                        text.setText("");
                        text1.setText("");
                        text2.setText("");
                    } else {
                        System.out.println("资源不存在");
                        itzDir.setVisible(false);
                        pre.setVisible(false);
                        next.setVisible(false);
                        cutNode.setVisible(false);
                        index = 1;
                        text.setText("资源图片不存在,请检查文件！");
                        text.setFill(Color.RED);
                    }
                } else {
                    System.out.println("主题路径不存在");
                    itzDir.setVisible(false);
                    pre.setVisible(false);
                    next.setVisible(false);
                    cutNode.setVisible(false);
                    index = 1;
                    text.setText("资源不存在，请检查文件！");
                    text.setFill(Color.RED);
                }
            }
        });
        ///压缩.itz
        itzDir.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            //拷贝
                            CopyDir copyDir = new CopyDir(themeDir, themeDir + "temp");
                            copyDir.copy();
                            System.out.println("cpoyok");
                            //压缩
                            new ItzCompress().compressDir(new File(themeDir + "temp"));
                            text1.setText("压缩完成");
                        } catch (Exception e) {
                            text1.setText("压缩失败！");
                            e.printStackTrace();
                        }

                    }
                }).start();

            }
        });

        pre.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                text2.setText("");
                index--;
                listen.switchPic(themeDir, rightVBox, index);
                if (index == 1) {
                    pre.setVisible(false);
                } else {
                    next.setVisible(true);
                }

            }
        });

        next.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                text2.setText("");
                index++;
                listen.switchPic(themeDir, rightVBox, index);
                if (index == 4) {
                    next.setVisible(false);
                } else {
                    pre.setVisible(true);
                }

            }
        });
//bug   节点截图
        cutNode.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                String s = themeDir.substring(0,themeDir.lastIndexOf("\\"));

//                WritableImage image = null;
//
//                    image = rightVBox.snapshot(new SnapshotParameters(), null);
                Image image =rightVBox.snapshot(null,null);

                    try {
                        File src = new File(s + "/"+ index + ".png");
                        ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png",src);
                        BufferedImage bf = ImageIO.read(new File(s + "/"+ index + ".png"));
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");//设置日期格式
                        File outputfile = new File(s + "/" + df.format(new Date()) + ".png");
                        ImageIO.write(bf.getSubimage(75,25,480,834), "png", outputfile);
                        text2.setText("截图成功，存储到" +outputfile.getAbsolutePath());
                        src.delete();
                    } catch (IOException ex) {
                        text2.setText("截取失败！");
                    }
            }
        });

        root.setLeft(leftVBox);
        root.setRight(rightVBox);
        root.setCenter(centerVBox);
        root.setTop(topVBox);
        root.setBottom(bottomVBox);

        Scene scene = new Scene(root, 1800, 1000);
        primaryStage.setTitle("Show");
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    /**
     * 检查文件夹
     */
    private boolean checkDir(String dir) {
        File file = new File(dir);
        if (file.list().length != 29) {
            return false;
        }
        for (int i = 0; i < ResName.dirNames.length; i++) {
            if (!new File(dir + "/" + ResName.dirNames[i]).exists()) {
                return false;
            }
        }
        return true;
    }



    public static void main(String[] args) {
        Application.launch(args);
    }
}


